﻿using CIS.Web.Common;
using CIS.Web.Properties;
using System;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace CIS.Web.Controllers
{
	[RoutePrefix("MeetingList")]
	public class MeetingListController : BaseDbController
	{
		public ActionResult Index()
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName);

			return View();
		}

		public async Task<JsonCamelCaseResult> GetData()
		{
			var sql = DbContext.ConferenceMaster.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常 && x.IsDisplay);

			if (Request.Cookies["_culture"] != null && Request.Cookies["_culture"].Value == ConstValues.Language.En)
			{
				sql = sql.Where(x => x.IsInternational);
			}
			else
			{
				sql = sql.Where(x => !x.IsInternational);
			}

			sql = sql.OrderByDescending(x => x.ConferenceStartDate).ThenBy(x => x.Sort);

			var sqlResult = await sql.ToListAsync();

			var result = sqlResult.Select(x => new
			{
				Id = x.ID,
				ConferenceName = x.ConferenceName,
				PicForPC = x.PicForPC,
				HomePageUrl = x.HomePageUrl,
				AllowContribute = x.ContributeStartDate.Value <= DateTime.Now && x.ContributeEndDate.Value >= DateTime.Now ? true : false,
				AllowRegister = x.RegisterStartDate.Value <= DateTime.Now && x.RegisterEndDate.Value >= DateTime.Now ? true : false,
				ConferenceStartDate = Request.Cookies["_culture"] != null && Request.Cookies["_culture"].Value == ConstValues.Language.En ?
							string.IsNullOrEmpty(x.OperateTime) ? string.Empty : x.OperateTime :
							x.ConferenceStartDate.Value.ToString("yyyy-MM-dd"),
				ConferenceEndDate = Request.Cookies["_culture"] != null && Request.Cookies["_culture"].Value == ConstValues.Language.En ?
							string.Empty :
							ConstValues.strSpace + Resources.To + ConstValues.strSpace + x.ConferenceEndDate.Value.ToString("yyyy-MM-dd"),
				StartYear = x.ConferenceStartDate.Value.Year.ToString(),
				StartMonth = Request.Cookies["_culture"] != null && Request.Cookies["_culture"].Value == ConstValues.Language.En ?
							x.ConferenceStartDate.Value.ToString("yyyy-MMM-dd").Substring(5, 3) :
							x.ConferenceStartDate.Value.Month.ToString("0#") + Resources.Month,
				StartDay = x.ConferenceStartDate.Value.Day.ToString("0#"),
				OperateLocation = x.OperateLocation,
				RegisterStartDate = x.RegisterStartDate.Value,
				RegisterEndDate = x.RegisterEndDate.Value,
				ContributeStartDate = x.ContributeStartDate.Value,
				ContributeEndDate = x.ContributeEndDate.Value,
				Status = x.Status,
				ConferenceModel = x.ConferenceModel,
				ConferenceNickName = x.ConferenceNickName,
				HasContribute = DbContext.ConferenceFunctionMaster.AsNoTracking()
						.Any(e => e.ConferenceId == x.ID &&
							e.Status == ConstValues.Status.正常 &&
							e.FunctionId == ConstValues.FunctionId.ConferenceContributeInfo),
				HasRegister = DbContext.ConferenceFunctionMaster.AsNoTracking()
						.Any(e => e.ConferenceId == x.ID &&
							e.Status == ConstValues.Status.正常 &&
							e.FunctionId == ConstValues.FunctionId.ConferenceAttendenceInfo),
				AllowHotel = DbContext.ConferenceFunctionMaster.AsNoTracking()
						.Any(e => e.ConferenceId == x.ID &&
							e.Status == ConstValues.Status.正常 &&
							e.FunctionId == ConstValues.FunctionId.ConferenceHotelEdit),
				AllowTraffic = DbContext.ConferenceFunctionMaster.AsNoTracking()
						.Any(e => e.ConferenceId == x.ID &&
							e.Status == ConstValues.Status.正常 &&
							e.FunctionId == ConstValues.FunctionId.ConferenceTraffic),
				HasHotel = DbContext.HotelMaster.AsNoTracking().FirstOrDefault(y => y.ConferenceId == x.ID && y.Status == ConstValues.Status.正常) == null ? false : true,
				HasTraffic = DbContext.TrafficMaster.AsNoTracking().FirstOrDefault(y => y.ConferenceId == x.ID && y.Status == ConstValues.Status.正常) == null ? false : true,
			});

			return JsonCamelCase(result, JsonRequestBehavior.AllowGet);
		}
	}
}